package com.hfi.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hfi.mapper.EmployeeMapper;
import com.hfi.model.Employee;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * @author ChangLiang
 * @date 2019/5/28
 */
@Service
public class EmployeeService {

    @Autowired
    private EmployeeMapper employeeMapper;

    public int addEmp(Employee employee) {
        // 输入json
        /*{
            "lastName":"changliang2",
                "email":"cl@hfi.com",
                "gender":"0"
        }*/

        // 执行的sql
        // INSERT INTO tbl_employee ( last_name, email, gender ) VALUES ( 'changliang2', 'cl@hfi.com', 0 )
        int insert = employeeMapper.insert(employee);



        System.out.println("插入完成后的主键值为："+employee.getId());
        return insert;
    }

    public int updateEmp(Employee employee) {
        int update = employeeMapper.updateById(employee);
        return update;
    }

    public Employee getEmployeeById(Integer id) {
        Employee employee = employeeMapper.selectById(id);
        return employee;
    }

    public Employee selectOneEmployee(Employee employee) {
        QueryWrapper<Employee> queryWrapper = new QueryWrapper<Employee>().eq("last_name", employee.getLastName());
        Employee one = employeeMapper.selectOne(queryWrapper);
        return one;
    }

    public List<Employee> selectBatchIdsEmployee(List<Integer> idList) {
        List<Employee> employees = employeeMapper.selectBatchIds(idList);
        return employees;
    }

    public List<Employee> selectByMapEmployee(Map<String, Object> map) {
        List<Employee> employees = employeeMapper.selectByMap(map);
        return employees;
    }

    public List<Employee> selectPageEmployee(Employee employee){
        /*return employeeMapper.selectPage(new Page<Employee>(employee.getPageNum(), employee.getPageSize()),
                new QueryWrapper<Employee>().eq("last_name", employee.getLastName())).getRecords();*/
        IPage<Employee> employeeIPage = employeeMapper.selectPage(new Page<Employee>(employee.getPageNum(),
                employee.getPageSize()), null);
        long total = employeeIPage.getTotal();
        System.out.println("total records: "+total);
        return employeeIPage.getRecords();
    }

    public List<Employee> selectPageEntityMapper(Employee employee) {
        List<Employee> records = employeeMapper.selectPage(new Page<Employee>(employee.getPageNum(),
                        employee.getPageSize()),
                new QueryWrapper<Employee>().between("age", 18, 50).eq("last_name", "changliang2").eq("gender", "0")).getRecords();
        return records;
    }

    public Integer updateEmpEntityMapper(Employee employee) {
        return employeeMapper.update(employee, new UpdateWrapper<Employee>().eq("last_name", "changliang"));
    }
}
